package nl.hanze.web.gba;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.hanze.web.gba.dao.DAOFacade;
import nl.hanze.web.gba.doa.impl.DAOFacadeImpl;
import nl.hanze.web.gba.domain.NatuurlijkPersoon;
import nl.hanze.web.gba.logic.Logic;

/**
 * Servlet implementation class GBA
 */
@WebServlet("/GBA")
public class GBA extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DAOFacade facade;
	private Logic logic;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GBA() {
        super();
        facade = new DAOFacadeImpl();
        logic = new Logic(facade);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=readAction(request);
        if ("get".equals(action)) {
        	
        	String bsn = request.getParameter("bsn");
        	NatuurlijkPersoon np = null;
        	if(bsn != null){
        		/*
        		 * BSN gegeven, zoeken op BSN
        		 */
        		np = logic.getNatuurlijkPersoonbyBSN(Long.parseLong(bsn));
        	}
        	
        	if(np == null) {
        		/*
        		 * Geen BSN gegeven, leeg NP met BSN = -1 teruggeven
        		 */
        		np = new NatuurlijkPersoon();
        		np.setBsn(-1);
        	}
        	// return json format
            request.setAttribute("np", np);
            RequestDispatcher view=request.getRequestDispatcher("get.jsp");
            view.forward(request, response);
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	private String readAction(HttpServletRequest request) {
        return request.getParameter("action");
    }

}
